//: C24:Reinterp.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// Reinterpret_cast
// Example depends on VPTR location,
// Which may differ between compilers.
#include <cstring>
#include <fstream>
using namespace std;
ofstream out("reinterp.out");

class X
{
    static const int sz = 5 ;
    int a[sz];
public:
    X() {
        memset(a, 0, sz * sizeof(int));
    }
    virtual void f() {}
    // Size of all the data members:
    int membsize() {
        return sizeof(a);
    }
    friend ostream&
    operator<<(ostream& os, const X& x) {
        for (int i = 0; i < sz; i++)
            os << x.a[i] << ' ';
        return os;
    }
    virtual ~X() {}
};

int main()
{
    X x;
    out << x << endl; // Initialized to zeroes
    int* xp = reinterpret_cast<int*>(&x);
    xp[1] = 47;
    out << x << endl; // Oops!

    X x2;
    const int vptr_size= sizeof(X) - x2.membsize();
    long l = reinterpret_cast<long>(&x2);
    // *IF* the VPTR is first in the object:
    l += vptr_size; // Move past VPTR
    xp = reinterpret_cast<int*>(l);
    xp[1] = 47;
    out << x2 << endl;
} ///:~
